<template>
  <div class="main">
    <el-form :model="ruleForm" status-icon ref="ruleForm" label-width="100px" class="demo-ruleForm"
             v-if="formVisible">
      <el-form-item label="用户名" prop="userName">
        <el-input v-model.number="ruleForm.username"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="pass">
        <el-input type="password" v-model="ruleForm.password" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item style="display: flex; justify-content: flex-end">
        <el-button type="primary" @click="submitForm()">提交</el-button>
      </el-form-item>
    </el-form>
    <div v-if="v2" style="display: flex; flex-direction: column; justify-content: space-between; align-items: center; height: 100px">
      <div>已登录</div>
      <el-button type="primary" @click="exit()">退出登录</el-button>
    </div>
  </div>

</template>

<script>
export default {
  name: "login",
  created() {
    let hasLogin = this.$store.state.hasLogin;
    this.formVisible = !hasLogin;
    this.v2 = hasLogin;
  },
  data() {
    return {
      formVisible: true,
      v2: false,
      ruleForm: {
        username: '',
        password: '',
      },
    };
  },
  methods: {
    exit(){
      this.formVisible = true;
      this.v2 = false;
      this.$store.commit('set_hasLogin',false)
    },
    async submitForm() {
      let res;
      try {
        res = await this.$axios.post("/user/login", this.ruleForm);
      } catch (e) {
        console.log(e)
      }
      this.formVisible = false;
      this.v2 = true;
      this.$store.commit('set_hasLogin',true)
      this.$message({
        type: 'success',
        message: '登录成功!'
      });
    },
  }

}
</script>

<style scoped>
.main {
  height: 100%;
  width: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
}
</style>
